#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
测试 @namespace 装饰器处理功能
"""

import ast

# IRBuilder已被移除，直接使用IRModule
from tests.pytests.helpers.codegen_helpers import compile_source, assert_code_contains

# 测试代码：包含 @namespace 装饰器的函数
test_code = """
from mcpy.decorators import namespace

@namespace("utils")
def utility_function_1() -> str:
    return "utility"

@namespace("utils")
def utility_function_2() -> str:
    return "utility"

@namespace("math.operations")
def calculate_1(x: int, y: int) -> int:
    return x + y

@namespace("math.operations")
def calculate_2(x: int, y: int) -> int:
    return x + y

@namespace()
def global_function() -> int:
    return 42

def regular_function() -> str:
    return "regular"
"""


def test_namespace_decorator_codegen():
    """测试 @namespace 装饰器的代码生成功能"""

    hpp, cpp = compile_source(test_code)

    expected_header = """
        namespace utils {
            std::string utility_function_1();
            std::string utility_function_2();
        } // namespace utils
        namespace math::operations {
            int calculate_1(int x, int y);
            int calculate_2(int x, int y);
        } // namespace math::operations
        namespace {
            int global_function();
        }
        std::string regular_function();
    """
    assert_code_contains(hpp, expected_header)

    expected_source = """
        namespace utils {
            std::string utility_function_1() {
                return "utility";
            }
            std::string utility_function_2() {
                return "utility";
            }
        } // namespace utils
        namespace math::operations {
            int calculate_1(int x, int y) {
                return x + y;
            }
            int calculate_2(int x, int y) {
                return x + y;
            }
        } // namespace math::operations
        namespace {
            int global_function() {
                return 42;
            }
        } // namespace
        std::string regular_function() {
            return "regular";
        }
    """
    assert_code_contains(cpp, expected_source)
